# -*- coding:utf-8 -*-

from app import orderhtml, salehtml, typepolarhtml, storehtml, storemonitor
from flask import Flask
from flask import send_file
from dataget import getrawdata
from datetime import timedelta


if __name__ == "__main__":

    app = Flask(__name__)
    app.config['DEBUG'] = True
    # 数据在浏览器缓存的时间：3分钟。三分钟之后调用视图函数返回浏览器中文件，而不重新获取数据
    app.config['SEND_FILE_MAX_AGE_DEFAULT'] = timedelta(seconds=60*3)
   
    @app.route('/')
    def app_0():
        return "<style>.content{height:800px;} a{ color:#333333;text-decoration: none;} a:visited {" \
               "color:#333333} </style>" \
               "<br><br><br><br><br><h1 align='center'>YumChina 订单数据</h1> <br><br><br>" \
               "<p style='text-align:center'><a href='/six'> <font size=5>获取数据</a> <br>" \
               "<a href='/one' target='_blank'> 当日订单总量及金额</a> <br>" \
               "<a href='/two' target='_blank'>当日订单类别分布</a> <br> " \
               "<a href='/three' target='_blank'>各市场订单类别</a> <br> " \
               "<a href='/four' target='_blank'>订单量排行前后二十门店</a> <br> " \
               "<a href='/five' target='_blank'>所有门店数据库心跳间隔监控</a></p> <br> "

    @app.route('/six')
    def app_6():
        global data2, searchtime
        data2, searchtime = getrawdata()
        return app_0()

    @app.route('/one')
    def app_1():
        try:
            salehtml(data2, searchtime)
            return send_file("sale.html")
        except NameError:
            return "请先获取数据"

    @app.route('/two')
    def app_2():
        try:
            orderhtml(data2, )
            return send_file("order.html")
        except NameError:
            return "请先获取数据"

    @app.route('/three')
    def app_3():
        try:
            typepolarhtml(data2)
            return send_file("typepolar.html")
        except NameError:
            return "请先获取数据"

    @app.route('/four')
    def app_4():
        try:
            storehtml(data2,searchtime)
            return send_file("topsale.html")
        except NameError:
            return "请先获取数据"

    @app.route('/five')
    def app_5():
        try:
            storemonitor(data2, searchtime)
            return send_file("storemonitor.html")
        except NameError:
            return "请先获取数据"

    app.run('0.0.0.0', port=222)

